package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.PaymentType;
import com.atguigu.lease.web.admin.service.PaymentTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "支付方式管理")
@RequestMapping("/admin/payment")
@RestController
public class PaymentTypeController {

    @Autowired
    private PaymentTypeService paymentTypeService;

    /**
     * 实现思路：
     * 单表操作
     * 查询单表的所有数据，没有分页
     * 实现过程：
     * mybatis-plus 的 service 扩展方法查询全部支付方式集合
     *
     * @return
     */
    @Operation(summary = "查询全部支付方式列表")
    @GetMapping("/list")
    public Result<List<PaymentType>> listPaymentType() {
        List<PaymentType> paymentTypeList = paymentTypeService.list();
        return Result.ok(paymentTypeList);
    }

    /**
     * 实现思路：
     * 单表的数据保存和更新
     * 有 id 更新
     * 没有 id 保存
     *
     * @param paymentType
     * @return
     */
    @Operation(summary = "保存或更新支付方式")
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdatePaymentType(@RequestBody PaymentType paymentType) {
        boolean saveOrUpdate = paymentTypeService.saveOrUpdate(paymentType);
        return saveOrUpdate ? Result.ok() : Result.fail();
    }

    @Operation(summary = "根据ID删除支付方式")
    @DeleteMapping("/deleteById")
    public Result deletePaymentById(@RequestParam Long id) {
        paymentTypeService.removeById(id);
        return Result.ok();
    }

}















